#!/usr/bin/make -f
# Simple set of debian/rules that uses debhelper.
# Based on an example by Joey Hess.
# extended by Paul Corner, Alex Joni, Chris Radek, Jeff Epler, John
# Morris (in cronological order)

# Copyright (C) 2006 Jeff Epler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# When used to produce a debian package, this file is a script "used to
# control compilation and installation of the executable"

# For cross-compiling
include /usr/share/dpkg/architecture.mk
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
DEB_BUILD_OPTIONS += nocheck
endif
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Enable/disable thread flavors; all flavors disabled by default
THREADS_POSIX = --without-posix
THREADS_RT_PREEMPT = --without-rt-preempt
THREADS_XENOMAI = --without-xenomai
#THREADS_XENOMAI_KERNEL = --without-xenomai-kernel
#THREADS_RTAI_KERNEL = --without-rtai-kernel

# Kernel threads need a list of configured header source directories
# for each architecture
#
# Multiples should be space-separated
#
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

#HEADERS_XENOMAI_KERNEL_i386 = --with-xenomai-kernel-sources=""
#HEADERS_XENOMAI_KERNEL_amd64 = --with-xenomai-kernel-sources=""
#HEADERS_RTAI_KERNEL_amd64 = --with-rtai-kernel-sources=""
#HEADERS_RTAI_KERNEL_i386 = --with-rtai-kernel-sources=""

# Parallel make
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif
# Only set 'CC' if explicitly defined in environment; otherwise 'make'
# chooses CC=cc and CXX=g++ for defaults
ifneq ($(origin CC),default)
    COMPILE_ENV += CC="$(CC)"
endif
ifneq ($(origin CXX),default)
    COMPILE_ENV += CXX="$(CXX)"
endif


debian/control: debian/configure
#	# if debian/configure has not been run, error out
	if ! test -f debian/configure.cmdline; then \
	    @echo "Please run debian/configure" \
	        "to generate the debian/control file."; \
	    false; \
	fi
	@echo rebuilding debian/control


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: debian/control
	dh_testdir

# Add here commands to compile the package.
	cd src && ./autogen.sh
	cd src && env $(COMPILE_ENV) \
	    ./configure --prefix=/usr \
	    --build=$(DEB_BUILD_MULTIARCH) \
	    --host=$(DEB_HOST_MULTIARCH) \
	    $(THREADS_POSIX) \
	    $(THREADS_RT_PREEMPT) \
	    $(THREADS_XENOMAI) \
	    --sysconfdir=/etc \
	    --mandir=/usr/share/man
	cd src && $(MAKE) $(COMPILE_ENV) $(MAKEFLAGS) V=$(DH_VERBOSE)
	touch build-stamp

clean: debian/control
	dh_testdir
	dh_testroot
	rm -f build-stamp

#	# Only run configure if the tree looks dirty. Running
#	# ./configure in qemu is expensive.
ifneq ($(wildcard src/configure src/Makefile.inc),)
	cd src && ./autogen.sh
	cd src && env $(COMPILE_ENV) \
	    ./configure --prefix=/usr \
	    --build=$(DEB_BUILD_MULTIARCH) \
	    --host=$(DEB_HOST_MULTIARCH) \
	    $(THREADS_POSIX) \
	    $(THREADS_RT_PREEMPT) \
	    $(THREADS_XENOMAI) \
	    --sysconfdir=/etc \
	    --mandir=/usr/share/man 
	cd src && $(MAKE) clean -s
#	# Manually remove things left by './configure'
	rm -f \
	    lib/python/machinekit/config.py \
	    lib/python/nf.py \
#	    scripts/check-build-vs-configure-sha \#
	    scripts/gen-rtapi.ini.sh \
	    scripts/gen-rtapi.ini.sh.in \
	    scripts/halrun \
	    scripts/haltcl \
	    scripts/linuxcnc \
	    scripts/linuxcnc_var \
	    scripts/linuxcncmkdesktop \
	    scripts/machinekit.ini.tmpl \
	    scripts/realtime \
	    scripts/rip-environment \
	    share/applications/linuxcnc-latency.desktop \
	    share/applications/linuxcnc.desktop \
	    share/desktop-directories/cnc.directory \
	    share/menus/CNC.menu \
	    src/aclocal.m4 \
	    src/config.h \
	    src/config.h.in \
	    src/config.log \
	    src/config.status \
	    src/configure \
	    src/machinekitcfg.py-tmp \
	    src/Makefile.inc \
	    src/Makefile.modinc \
	    tcl/linuxcnc.tcl
	rm -rf src/autom4te.cache etc

endif

#	# Remove package artifacts
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
# start the install
	mkdir -p debian/tmp
	(cd debian/extras && cp -a * ../tmp)
	(cd src; export DESTDIR=`pwd`/../debian/tmp; $(MAKE) V=$(DH_VERBOSE) $@)
	mkdir -p debian/tmp/usr/lib debian/tmp/usr/include/linuxcnc
	cp lib/*.a debian/tmp/usr/lib
	## if they weren't there we don't care if rm fails
	rm -f debian/tmp/usr/share/applications/linuxcnc-usermanual*.desktop
	rm -f debian/tmp/usr/share/applications/linuxcnc-halmanual*.desktop
	rm -f debian/tmp/usr/share/applications/linuxcnc-integratormanual*.desktop
	rm -f debian/tmp/usr/share/applications/linuxcnc-gettingstarted*.desktop
	rm -f debian/tmp/usr/share/applications/linuxcnc-developer*.desktop
	rm -rf debian/tmp/usr/share/doc/linuxcnc/html
	rm -f debian/tmp/usr/share/linuxcnc/examples/sample-configs/*/*position*.txt
# avoid clashes with machinekit-hal
	rm -f debian/tmp/usr/share/linuxcnc/Makefile.*
	rm -f debian/tmp/usr/include/linuxcnc/config.h
	# update emcweb.hal
	sed 's/#NC_FILES_DIR/NC_FILES_DIR/' -i debian/tmp/usr/share/linuxcnc/examples/sample-configs/sim/emcweb.ini

ifneq (,$(wildcard debian/machinekit-*-kernel-*.install))
	mkdir -p debian/tmp/etc/udev/rules.d
	cp src/rtapi/shmdrv/shmdrv.rules \
	    debian/tmp/etc/udev/rules.d/50-shmdrv.rules
endif
	cp debian/machinekit-cnc-posix.install.in debian/machinekit-cnc-posix.install
	cp debian/machinekit-cnc-rt-preempt.install.in debian/machinekit-cnc-rt-preempt.install
	cp debian/machinekit-cnc-xenomai.install.in debian/machinekit-cnc-xenomai.install

	dh_install --sourcedir=debian/tmp --fail-missing -Xusr/bin/pasm

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installexamples
	dh_installman
	dh_installmime
	dh_link
	dh_strip
	dh_compress -X.pdf -X.txt -X.hal -X.ini -X.clp -X.var -X.nml \
	    -X.tbl -X.xml -Xsample-configs
	dh_fixperms -X/linuxcnc_module_helper -X/rtapi_app_
	dh_python2 --ignore-shebangs --no-guessing-versions --no-ext-rename
	dh_makeshlibs
	dh_installdeb

#	# delete files that should be in machinekit-hal-<flavor> package
	rm -f debian/machinekit/usr/bin/comp
	rm -f debian/machinekit/usr/share/linuxcnc/Makefile.modinc
	rm -f debian/machinekit/usr/share/linuxcnc/Makefile.inc

	#disable dh_shlibs, it is completely confused by linkage to libs not in packages
	##dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info -l debian/machinekit/usr/lib
	dh_gencontrol
	
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
